package com.mach.platform.repository;

import com.mach.platform.domain.base.Account;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.Map;

/**
 * @author Anonymous
 * @Description:  用户数据库操作
 * @Package: com.mach.platform.com.mach.platform.repository
 * @time 2018/04/03 22:53
 */
@Repository
public interface AccountRepo extends BaseRepository<Account> {

	/**
	 * 修改密码
	 * @param id 用户id
	 * @param newPassword 新密码
	 * @return 影响数
	 */
	@Modifying
	@Query(value = "update account set password=?2 where id=?1",nativeQuery = true)
	Integer changePassword (Long id,String newPassword );


	@Query(value = "select password from account where id = ?1",nativeQuery = true)
	String getPassword (Long id);

	/**
	 *清空传入 用户数组ids的组织架构信息
	 * @param ids  用户id
	 * @return Integer
	 */
	@Modifying
	@Query(value = "update account set orgName=null ,orgId=null where id in (?1)", nativeQuery = true)
	Integer setOrgInfoNull (Long[] ids);

	/**
	 * 清空传入 用户数组ids的岗位信息
	 * @param ids 用户id
	 * @return Integer
	 */
	@Modifying
	@Query(value = "update account set postName=null ,postId=null where id in (?1)", nativeQuery = true)
	Integer setPostInfoNull (Long[] ids);

	/**
	 * 查找 对应角色id的 对用用户数据
	 * @param roleId  角色id
	 * @param searchParam  模糊查询名字
	 * @param pageable  分页参数
	 * @return Page<Map>
	 */
	@Query(value = "select id,name,enabled,username,mobile,postName,orgName,gender,age,email from account where id in (select distinct ar.account_id from role_account ar where ar.role_id=?1) and name like concat('%',?2,'%')",
			countQuery = "select count(*) from account where id in (select distinct ar.account_id from role_account ar where ar.role_id=?1) and name like concat('%',?2,'%')",nativeQuery = true)
	Page<Map> loadUserByRoleIdIn (Long roleId, String searchParam,Pageable pageable);

	/**
	 * 查找 对应角色id的 所管理之外的 用户数据
	 * @param roleId  角色id
	 * @param searchParam  模糊查询名字
	 * @param pageable  分页参数
	 * @return Page<Map>
	 */
	@Query(value = "select id,name,enabled,username,mobile,postName,orgName,gender from account where id not in (select distinct ar.account_id from role_account ar where ar.role_id=?1) and name like concat('%',?2,'%')",
			countQuery = "select count(*) from account where id not in (select distinct ar.account_id from role_account ar where ar.role_id=?1) and name like concat('%',?2,'%')",nativeQuery = true)
	Page<Map> loadUserByRoleIdOut (Long roleId, String searchParam,Pageable pageable);

	/**
	 * 删除用户的时候，同时删除用户-角色关系表的数据
	 * @param userIds
	 * @return
	 */
	@Modifying
	@Query(value = "delete from role_account where account_id in ?1",nativeQuery = true)
	Integer deleteRelationWhileDeleteUser (Long[] userIds);

	/**
	 *
	 * @param roleName  角色名称
	 * @param pageable  分页参数
	 * @return Page<Map>
	 */
	@Query(value = "select ac.name from role_account as ra\n" +
			"left join role as ro on ra.role_id = ro.id\n" +
			"left join account as ac on ra.account_id = ac.id\n" +
			"where ro.name = ?1",
			countQuery = "select count(*) from role_account as ra\n" +
					"left join role as ro on ra.role_id = ro.id\n" +
					"left join account as ac on ra.account_id = ac.id\n" +
					"where ro.name = ?1",nativeQuery = true)
	Page<Map> getAccountByRoleName(String roleName, Pageable pageable);
}
